package com.itboye.lottery.widget.indexfragmentbanner;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.itboye.lottery.R;


public class BezierViewPager extends ViewPager {

    private boolean touchable = true;
    private boolean loopEnable = false;//是否自动轮播
    private long durating = 2000;// 切换间隔
    private boolean isTouching = false;
    private LinearLayout indicator;

    public boolean isLoopEnable() {
        return loopEnable;
    }

    public void setLoopEnable(boolean loopEnable) {
        this.loopEnable = loopEnable;
    }

    public long getDurating() {
        return durating;
    }

    public void setDurating(long durating) {
        this.durating = durating;
    }

    private ShadowTransformer cardShadowTransformer;

    public BezierViewPager(Context context) {
        super(context);
        initPageChangeListener();
    }

    public BezierViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPageChangeListener();

    }

    private void initPageChangeListener() {
        addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setSelctIndicator();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                if (!isTouching) {
                    setCurrentItem((getCurrentItem() + 1) % (getAdapter()).getCount());
                }
                startLoop();
            } catch (Exception e) {

            }
        }

        ;
    };

    public void startLoop() {
        if (isLoopEnable()) {
            handler.sendEmptyMessageDelayed(1, durating);
        }
    }

    public void setTouchable(boolean isCanScroll) {
        this.touchable = isCanScroll;
    }

    @Override
    public void scrollTo(int x, int y) {
        super.scrollTo(x, y);
    }

    @Override
    public boolean onTouchEvent(MotionEvent arg0) {
        if (touchable) {
            isTouching = true;
            switch (arg0.getAction()) {
//                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    isTouching = false;
                    break;
            }
            Log.v("view_pager_touch", ">>>>>>>>正在触摸");
            return super.onTouchEvent(arg0);
        } else {
            return false;
        }

    }

    @Override
    public void setCurrentItem(int item, boolean smoothScroll) {
        super.setCurrentItem(item, smoothScroll);
    }

    @Override
    public void setCurrentItem(int item) {
        super.setCurrentItem(item);
    }

    private void setSelctIndicator() {
        for (int i = 0; i < indicator.getChildCount(); i++) {
            ImageView imageView = (ImageView) indicator.getChildAt(i);
            imageView.setSelected(getCurrentItem() == i);
            Log.v("view_pager_touch", (getCurrentItem() == i) + "getCurrent:" + getCurrentItem() + "   i:" + i);
        }

    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        if (touchable) {
            return super.onInterceptTouchEvent(arg0);
        } else {
            return false;
        }

    }

    public void showTransformer(float zoomIn) {
        if (CardAdapter.class.isInstance(getAdapter())) {
            if (cardShadowTransformer == null) {
                cardShadowTransformer = new ShadowTransformer();
                cardShadowTransformer.attachViewPager(this, (CardAdapter) getAdapter());
            }
            cardShadowTransformer.setZoomIn(zoomIn);

        }
    }

    @SuppressLint("NewApi")
    public void setIndicatorView(LinearLayout indicator) {
        this.indicator = indicator;
        indicator.removeAllViews();
        for (int i = 0; i < getAdapter().getCount(); i++) {
            Log.v("view_pager_touch", ">>>>>>>>创建indicator");
            ImageView imageView = new ImageView(getContext());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 4);
            layoutParams.setMargins(6, 2, 6, 2);
            imageView.setLayoutParams(layoutParams);
            imageView.setBackground(getResources().getDrawable(R.drawable.indicator_selector));
            indicator.addView(imageView);
        }
    }
}
